package com.axc.gallery2.net;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import com.axc.gallery2.view.UploadPhotosAdapter;

public class UploadAdapterHelper {

	private UploadAdapterHelper(){

	}

	static UploadPhotosAdapter uploadPhotosAdapter;
	//保存上传任务
	public static void setAdpter(UploadPhotosAdapter adapter,Context context) {
		uploadPhotosAdapter =adapter;
		CookieSyncManager.createInstance(context);
		CookieManager cookieManager =CookieManager.getInstance();
		StringBuilder sbBuilder =new StringBuilder();
		for (String item : adapter.getList()) {
			sbBuilder.append(item);
			sbBuilder.append("|");
			System.out.println("setcookies:"+item);
		}
		System.out.println("setcookies:"+sbBuilder.toString());
		cookieManager.setCookie(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera", sbBuilder.toString());
	}

	//获取上传任务
	public static UploadPhotosAdapter getAdapter(Context context) {

		if(uploadPhotosAdapter==null){
			List<String> list =new ArrayList<String>();
			CookieSyncManager.createInstance(context);
			CookieManager cookieManager =CookieManager.getInstance();
			String cookies = cookieManager.getCookie(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera");
			System.out.println("cookies:"+cookies);
			if(cookies!=null){
				for (String item : cookies.split("|")) {
					if(!item.trim().equals("")&&item.length()>3){
						list.add(item);
						System.out.println("cookies+item:"+item);
					}
				}
			}
			uploadPhotosAdapter =new UploadPhotosAdapter(context, list);
		}
		return uploadPhotosAdapter;
	}
}
